package com.laikan.reader.bookfriend.holder;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.laikan.reader.R;
import com.laikan.reader.bookfriend.entity.PageAutherBean;
import com.laikan.reader.bookfriend.mactivity.ContentActivity;
import com.laikan.reader.bookfriend.mactivity.AutherActivity;
import com.laikan.reader.bookfriend.utils.Constant;
import com.laikan.reader.bookfriend.utils.ImageUtils;
import com.laikan.reader.core.base.BaseHolder;

/**
 * Created by lingyt on 2016/11/16.
 * 头部的Header
 */
public class HeaderHolder extends BaseHolder implements View.OnClickListener {
    private SimpleDraweeView mBgTitle;
    private SimpleDraweeView mIvFace;
    private TextView mTvFocus;
    private TextView mTvFans;
    private TextView mTvFocusNum;
    private TextView mTvFansNum;
    public Button mBtFocus;
    private TextView mTvDes;
    private TextView mTvAutherName;
    private AutherActivity mActivity;

    public HeaderHolder(Context context){
        if (context instanceof AutherActivity){
            mActivity = (AutherActivity) context;
        }
    }

    @Override
    public View initView() {
        View view = View.inflate(ImageUtils.getContext(), R.layout.view_person_header,null);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ImageUtils.getDimens(R.dimen.view_person_header_height));
        view.setLayoutParams(params);

        mBgTitle = (SimpleDraweeView) view.findViewById(R.id.bg_title_image);
        ImageUtils.frescoBlur(mBgTitle,ImageUtils.getContext(),R.drawable.lk_default_personal_image_blue);

        mIvFace = (SimpleDraweeView)view.findViewById(R.id.iv_face);//作者圆形头像
        mTvFocus = (TextView) view.findViewById(R.id.tv_focus_text);//关注文本
        mTvFans = (TextView) view.findViewById(R.id.tv_fans_text);//粉丝文本
        mTvFocusNum = (TextView) view.findViewById(R.id.tv_focus_number);//关注量
        mTvFansNum = (TextView) view.findViewById(R.id.tv_fans_number);//粉丝量
        mBtFocus = (Button) view.findViewById(R.id.bt_focus);//关注按钮
        mTvDes = (TextView) view.findViewById(R.id.tv_des);//个人签名
        mTvAutherName = (TextView) view.findViewById(R.id.tv_auther);//作者姓名


        mTvFans.setOnClickListener(this);
        mTvFocus.setOnClickListener(this);
        return view;
    }

    @Override
    public void refreshView() {
        PageAutherBean bean = (PageAutherBean) getData();

        mIvFace.setImageURI(Uri.parse(bean.data.authorPic));
        mTvAutherName.setText(bean.data.authorName);
        mTvDes.setText(bean.data.authorIntro);
        mTvFansNum.setText(bean.data.funsNum);
        mTvFocusNum.setText(bean.data.attentionNum);

        ImageUtils.frescoBlur(mBgTitle,ImageUtils.getContext(),bean.data.authorPic);
        setHeaderStatus(bean.data.isMyself);
        setAttentionButtonStatus(bean.data.isAttention);

    }

    public void setAttentionButtonStatus(boolean isAttention) {
        mBtFocus.setText(isAttention?"已关注":"关注");
    }

    private void setHeaderStatus(boolean isMyself) {
        if (isMyself) {
            mTvFans.setText("我的粉丝");
            mTvFocus.setText("我的关注");
            mBtFocus.setVisibility(View.INVISIBLE);
        }else{
            mTvFans.setText("粉丝");
            mTvFocus.setText("关注");
            mBtFocus.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.tv_focus_text://跳转到关注页

                intent.setClass(ImageUtils.getContext(),ContentActivity.class);
                intent.putExtra(ContentActivity.PAGE_TYPE, ContentActivity.PAGE_TYPE_FOCUS);
                intent.putExtra(Constant.USER_ID,mActivity.getAuthorId());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ImageUtils.getContext().startActivity(intent);
                break;
            case R.id.tv_fans_text://跳转到粉丝页
                intent.setClass(ImageUtils.getContext(),ContentActivity.class);
                intent.putExtra(ContentActivity.PAGE_TYPE, ContentActivity.PAGE_TYPE_FANS);
                intent.putExtra(Constant.USER_ID,mActivity.getAuthorId());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ImageUtils.getContext().startActivity(intent);

                break;
        }
    }
}
